<?php

function isLogin()
{
	
	if(session('openid') == '' && session('shop_id')){

		to();
	}elseif(session('shop_id') == '' && session('openid')){

		$user = M('shop') -> where(array('openid' => session('openid'))) -> getField('shop_id');
		if($user){
			session('shop_id',$user);
		}else{
			return 1;
		}
	}elseif(session('shop_id') == '' && session('openid') == ''){		
		return 1;
	}else{

		shopPoints(session('shop_id'));
	}
	
}


function to()
{
	$mysql_server_name='127.0.0.1'; //改成自己的mysql数据库服务器
 
	$mysql_username='root'; //改成自己的mysql数据库用户名
	 
	$mysql_password='root'; //改成自己的mysql数据库密码
	 
	$mysql_database='cdzx'; //改成自己的mysql数据库名



	$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("数据库链接错误") ; //连接数据库
	 
	mysql_query("set names 'utf8'"); //数据库输出编码 
	 
	mysql_select_db($mysql_database); //打开数据库
	 
	$sql ="select * from app "; //SQL语句
	 
	$result = mysql_query($sql,$conn); //查询
	$row = mysql_fetch_array($result);

	$appId = $row['appId'];
	
	$encrypt = urlEncode('http://cdzx.laobing2016.com/index.php/Seller/Login/index');
	$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.'&redirect_uri='.$encrypt.'&response_type=code&scope=snsapi_base&state=123#wechat_redirect '; 
	header('Location:'.$url);
}

/*计算返还积分*/
function retPoints($shopid)
{
	$reModel = M('recharge');
	$shopModel = M('shop');

	$arr['shop_id'] = $shopid;
	$reShop = $shopModel->where($arr)->select();
	$arr['reason'] = '赠送积分';
	$res = $reModel->where($arr)->select();

	//定义一个变量，用于保存积分
	$re['return_points'] = 0;
	for ($i=0; $i < count($res); $i++) { 
		$re['return_points'] = $res[$i]['points'] + $re['return_points'];
	}
	$Model = new \Think\Model();
	$new_point = $Model->execute("update shop set return_points=".$re['return_points']." where shop_id = ".$arr['shop_id']);
	
}

/*计算可提现积分*/
function shopPoints($shopid)
{
	retPoints($shopid);
	$payModel = M('pay');
	$shopModel = M('shop');

	//查询信息
	$arr['shop_id'] = $shopid;
	$resShop = $shopModel->where($arr)->select();

	/*商家提现通过的记录*/
    $arr['is_user'] = 0;
    $arr['apply'] = 1;
    $arr['isApply'] = 1;
    
    $payRes = $payModel->where($arr)->select();

    $res = array();
    for ($i=0; $i < count($payRes); $i++) { 
        //判断此记录是不是提现记录
        $resMoney = strstr($payRes[$i]['money'], '-');
        if($resMoney)
        {
			array_push($res,$payRes[$i]);
            //$res[$i] = $payRes[$i];
        }
    }
	if($resShop[0]['return_points'])
	{
		$pay['use_points'] = $resShop[0]['return_points'];
	}else{
		$pay['use_points'] = 0;
	}
	for ($i=0; $i < count($res); $i++) { 
		$pay['use_points'] = $res[$i]['money'] + $pay['use_points'];
	}
	/*提现记录end*/

	/*提现申请中的的记录*/
  	$arr1['shop_id'] = $shopid;
    $arr1['is_user'] = 0;
    $arr1['apply'] = 0;
    $arr1['isApply'] = 0;
    
    $payRes1 = $payModel->where($arr1)->select();

    $res1 = array();
    for ($i=0; $i < count($payRes1); $i++) { 
        //判断此记录是不是提现记录
        $resMoney1 = strstr($payRes1[$i]['money'], '-');
        if($resMoney1)
        {
			array_push($res1,$payRes1[$i]);
            //$res1[$i] = $payRes1[$i];
        }
    }
	
	for ($i=0; $i < count($res1); $i++) { 
		$pay['use_points'] = $res1[$i]['money'] + $pay['use_points'];
	}
	/*提现记录end*/
	$Model = new \Think\Model();
	$new_point = $Model->execute('update shop set use_points='.$pay['use_points'].' where shop_id='.$arr1['shop_id']);
	
}

/*计算会员的相关信息*/
//计算消费积分
function upoints($userid)
{
	$logModel = M('log');
	$userModel = M('user');

	$arr['user_Id'] = $userid;
	$arr['reason'] = '消费积分';
	$res = $logModel->where($arr)->select();

	//定义一个变量，用于保存积分
	$log['pay_points'] = 0;
	for ($i=0; $i < count($res); $i++) { 
		$log['pay_points'] = $res[$i]['points'] + $log['pay_points'];
	}

	//计算有几个积分单元
	$configModer = M('config');
	$configRes   = $configModer->select();
    $weight = $configRes[0]['weight'];
	
	$log['unit'] = floor($log['pay_points']/$weight);
	if($log['unit'] < 0)
	{
		$log['unit'] = 0;
	}

	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set pay_points=".$log['pay_points'].", unit =".$log['unit']." where user_Id = ".$arr['user_Id']);

}
//计算可返积分单元
function num($id)
{
	upoints($id);
	$arr['user_id'] = $id;
	$arr['is_user'] = 1;
	$weightModel = M('weight');
    $weightRes = $weightModel->where($arr)->select();
    $back = 0;
    for ($i=0; $i < count($weightRes); $i++) { 
		$back = $weightRes[$i]['back'] + $back;
	}
	$configModer = M('config');
	$configRes   = $configModer->select();
    $weight = $configRes[0]['weight'];
    $unit = floor($back/$weight);
	if($unit < 0)
	{
		$unit = 0;
	}

	$userModel = M('user');
	$user['user_Id'] = $id;
	$uRes = $userModel->where($user)->select();

	$user['isunit'] = $uRes[0]['unit'] - $unit;
	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set isunit =".$user['isunit']." where user_Id = ".$user['user_Id']);
}


/*计算返还积分*/
function uretPoints($userid)
{
	num($userid);
	$logModel = M('log');
	$userModel = M('user');

	$arr['user_Id'] = $userid;
	$resUser = $userModel->where($arr)->select();
	$arr['reason'] = '赠送积分';
	$res = $logModel->where($arr)->select();

	//定义一个变量，用于保存积分
	$log['return_points'] = 0;
	for ($i=0; $i < count($res); $i++) { 
		$log['return_points'] = $res[$i]['points'] + $log['return_points'];
	}

	/*计算weight里面的返还积分*/
	$wModel = M('weight');
	$w['user_id'] = $userid;
	$w['is_user'] = 1;
	$wRes = $wModel->where($w)->select();
	for ($j=0; $j < count($wRes); $j++) { 
		$log['return_points'] = $wRes[$j]['back'] + $log['return_points'];
	}
	
	/*计算weight里面的返还积分end*/

	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set return_points= '".$log['return_points']."' where user_Id = ".$arr['user_Id']);
}

/*计算可提现积分*/
function uusePoints($userid)
{
	uretPoints($userid);
	$payModel = M('pay');
	$userModel = M('user');

	//查询会员信息
	$arr['user_Id'] = $userid;
	$resUser = $userModel->where($arr)->select();
	/*会员提现通过的记录*/
  	$arr['user_id'] = $userid;
    $arr['is_user'] = 1;
    $arr['apply'] = 1;
    $arr['isApply'] = 1;
    
    $payRes = $payModel->where($arr)->select();

    $res = array();
    for ($i=0; $i < count($payRes); $i++) { 
        //判断此记录是不是提现记录
        $resMoney = strstr($payRes[$i]['money'], '-');
        if($resMoney)
        {
            $res[$i] = $payRes[$i];
        }
    }
	
	$pay['use_points'] = $resUser[0]['return_points'];
	for ($i=0; $i < count($res); $i++) { 
		$pay['use_points'] = $res[$i]['money'] + $pay['use_points'];
	}
	/*会员提现记录end*/

	/*会员提现申请中的的记录*/
  	$arr1['user_id'] = $userid;
    $arr1['is_user'] = 1;
    $arr1['apply'] = 0;
    $arr1['isApply'] = 0;
    
    $payRes1 = $payModel->where($arr1)->select();

    $res1 = array();
    for ($i=0; $i < count($payRes1); $i++) { 
        //判断此记录是不是提现记录
        $resMoney1 = strstr($payRes1[$i]['money'], '-');
        if($resMoney1)
        {
            $res1[$i] = $payRes1[$i];
        }
    }
	
	for ($i=0; $i < count($res1); $i++) { 
		$pay['use_points'] = $res1[$i]['money'] + $pay['use_points'];
	}
	/*会员提现记录end*/

	$Model = new \Think\Model();
	$new_point = $Model->execute("update user set use_points= '".$pay['use_points']."' where user_Id = ".$arr1['user_id']);
}